<template>
    <div>
      <!-- 导航栏 -->
      <van-nav-bar left-arrow @click-left="onClickLeft">
            <template #title>
                <span>编辑地址</span>
            </template>
      </van-nav-bar>

      <van-address-edit
        :area-list="areaList"
        show-delete
        show-set-default
        show-search-result
        :address-info="addressInfo"
        :search-result="searchResult"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
        @change-detail="onChangeDetail"
      />
    </div>
</template>

<script>
import { Toast } from 'vant';
import { areaList } from '@vant/area-data';
import axios from 'axios'
export default {
  data() {
    return {
      areaList,
      searchResult: [],
      addressInfo:{}
    };
  },
  props:['addressId'],
  methods: {
    onClickLeft(){
      // 后退一步
      this.$router.go(-1);
    },
    onSave(address) {
      address.isDefault = address.isDefault ? 1 : 0;
      this.$set(address,"uid",this.$store.state.uid)
      console.log(JSON.stringify(address));
      // axios.defaults.headers['Content-type'] = 'application/x-www-form-urlencoded;charset=utf-8';
      axios.post('http://localhost:9090/api/updateAddressById',{
          address:JSON.stringify(address)
        })        
        .then(res => {    
          // 处理响应数据
          if (res.data.code === 200) {
            Toast('编辑成功');
            this.$router.go(-1)
          }else{
            Toast('编辑失败');
          }
        })        
        .catch(err => {            
            console.log(err.data)        
        }) 
    },
    onDelete() {
      Toast('delete');
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: '黄龙万科中心',
            address: '杭州市西湖区',
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
    queryAddress(){
      let that = this
      axios.get('http://localhost:9090/api/queryAddressById',{
        params:{
          addressId:that.addressId
        }
      })
      .then(response => {
          // 处理响应数据
          console.log(response.data.data);
          that.addressInfo = response.data.data;
          if(that.addressInfo.isDefault === 1){
            that.addressInfo.isDefault = true;
          }
      })
      .catch(error => {
          // 处理错误
          console.error(error);
      });
    }
  },
  mounted(){
    this.queryAddress()
  }
};
</script>

<style>

</style>